//
//  ViewController.m
//  mousetransportChesse
//
//  Created by student on 16/3/23.
//  Copyright © 2016年 NIIT. All rights reserved.
//


// 练习:
// 一个盒子里有100块奶酪,有个鼠洞通到盒子
// 3只老鼠,老鼠需要把奶酪叼回自己的窝里，每只老鼠搬得速度不同。(起始的时候，每只老鼠窝里有10块奶酪)
// 鼠洞很小，每一刻只能有一只老鼠在里面通行。（搬奶酪过程，线程互斥）
// 每只老鼠跑动速度不同(来回搬一次时间为0.3s 0.4s 0.5s)
// 每只老鼠搬完了要休息(0.2s)
// 老鼠发现如果不能叼奶酪，就是在窝里吃0.1块奶酪(耗费每0.1秒),然后继续尝试能不能叼
// 显示最后3只老鼠窝里的奶酪数量，如果没得吃，那只老鼠饿死。
// 线程结束条件:盒子里奶酪没了,或者饿死了。


#import "ViewController.h"

@interface ViewController ()

@property (nonatomic,assign) int cheeseNumber;
@property (nonatomic,strong) NSLock *lock;
@property (nonatomic,assign) int mouseCheese1;
@property (nonatomic,assign) int mouseCheese2;
@property (nonatomic,assign) int mouseCheese3;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.cheeseNumber = 100;
    self.mouseCheese2 = 10;
    self.mouseCheese3 = 10;
    self.mouseCheese1 = 10;
    
    self.lock = [[NSLock alloc] init];
    
    
    NSThread *mousethread1 = [[NSThread alloc] initWithTarget:self selector:@selector(takeCheese) object:nil];
    mousethread1.name = @"锦毛鼠";
    NSThread *mousethread2 = [[NSThread alloc] initWithTarget:self selector:@selector(takeCheese) object:nil];
    mousethread2.name = @"仓鼠";
    NSThread *mousethread3 = [[NSThread alloc] initWithTarget:self selector:@selector(takeCheese) object:nil];
    mousethread3.name = @"挪威鼠";
    [mousethread1 start];
    [mousethread2 start];
    [mousethread3 start];
}

- (void)takeCheese
{
    while (1)
    {
        if ([self.lock tryLock])
        {
            if(self.cheeseNumber>0)
            {
                if([[NSThread currentThread].name isEqualToString:@"锦毛鼠"])
                {
                    [NSThread sleepForTimeInterval:0.3];
                    self.cheeseNumber = self.cheeseNumber-1;
                    self.mouseCheese1 = self.mouseCheese1 + 1;
                    [self.lock unlock];
                    NSLog(@"%@:搬了一块奶酪，剩余：%i",[NSThread currentThread].name,self.cheeseNumber);
                    [NSThread sleepForTimeInterval:0.2];
                    NSLog(@"%@:休息了0.2秒",[NSThread currentThread].name);
                    
                }
                else if([[NSThread currentThread].name isEqualToString:@"仓鼠"])
                {
                    [NSThread sleepForTimeInterval:0.4];
                    self.cheeseNumber = self.cheeseNumber-1;
                    self.mouseCheese2 = self.mouseCheese2 + 1;
                    [self.lock unlock];
                    NSLog(@"%@:搬了一块奶酪，剩余：%i",[NSThread currentThread].name,self.cheeseNumber);
                    [NSThread sleepForTimeInterval:0.2];
                    NSLog(@"%@:休息了0.2秒",[NSThread currentThread].name);
                }
                else
                {
                    [NSThread sleepForTimeInterval:0.5];
                    self.cheeseNumber = self.cheeseNumber-1;
                    self.mouseCheese3 = self.mouseCheese3 + 1;
                    [self.lock unlock];
                    NSLog(@"%@:搬了一块奶酪，剩余：%i",[NSThread currentThread].name,self.cheeseNumber);
                    [NSThread sleepForTimeInterval:0.2];
                    NSLog(@"%@:休息了0.2秒",[NSThread currentThread].name);
                }
            }
            else
            {
                NSLog(@"%@:奶酪搬光了",[NSThread currentThread].name);
                [self.lock unlock];
                [NSThread exit];
            }
        }
        else
        {
            if(self.mouseCheese1<=0 || self.mouseCheese2<=0 || self.mouseCheese3<=0)
            {
                NSLog(@"没有奶酪，老鼠饿死了");
            }
            else
            {
                if([[NSThread currentThread].name isEqualToString:@"锦毛鼠"])
                {
                    [NSThread sleepForTimeInterval:0.1];
                    self.mouseCheese1 = self.mouseCheese1*0.1;
                    NSLog(@"%@:吃了0.1块奶酪，剩余：%i",[NSThread currentThread].name,self.mouseCheese1);
                }
                else if([[NSThread currentThread].name isEqualToString:@"仓鼠"])
                {
                    [NSThread sleepForTimeInterval:0.1];
                    self.mouseCheese2 = self.mouseCheese2*0.1;
                    NSLog(@"%@:吃了0.1块奶酪，剩余：%i",[NSThread currentThread].name,self.mouseCheese2);
                }
                else
                {
                    [NSThread sleepForTimeInterval:0.1];
                    self.mouseCheese3 = self.mouseCheese3*0.1;
                    NSLog(@"%@:吃了0.1块奶酪，剩余：%i",[NSThread currentThread].name,self.mouseCheese3);
                }
            }
        }
    }
}

@end
